package com.yunfeisoft.utils;

import com.applet.utils.DateUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by Jackie Liu on 2017/6/28.
 */
public class Test {/*

    static String middle_ftpServer = "192.168.11.251";
    static String middle_user = "cs";
    static String middle_password = "123";
    static String middle_dir = "/image/NWKPHOTO/Middle/2009/3";

    public static void main(String[] args) {
        FTPClientConfigure configure = new FTPClientConfigure();
        configure.setHost("192.168.11.110");
        configure.setUserName("cs");
        configure.setPassword("123");
        configure.setClientTimeout(120000);

        FtpFileOperation operation = new FtpFileOperation();
        operation.setConfigure(configure);
        operation.setSaveFilePath("/image");
        operation.afterPropertiesSet();
        try {
            operation.save(new File("E:\\jaas\\jaas.config"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //operation.read("/image/aa.config");
        byte[] result = operation.read("/image/aa.txt");
        System.out.println(result.length);
        operation.close();
        System.out.println("aaaaaaaaaa");

        String sql = "select a.name, b.name FROM users a, usergroups b on a.groupId = b.id where a.groupID = ?";
        //sql = "insert into table1 (id, name) values (?, ?)";

        MySqlStatementParser parser = new MySqlStatementParser(sql);
        List<SQLStatement> statementList = parser.parseStatementList();
        SQLStatement statemen = statementList.get(0);

        MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
        statemen.accept(visitor);

        System.out.println(sql);
        System.out.println("getTables : " + visitor.getTables());
        System.out.println("getColumns : " + visitor.getColumns());
        System.out.println("getConditions : " + visitor.getConditions());
        System.out.println("getRelationships : " + visitor.getRelationships());
        System.out.println("getCurrentTable : " + visitor.getCurrentTable());
        System.out.println("getAliasMap : " + visitor.getAliasMap());
        System.out.println("getGroupByColumns : " + visitor.getGroupByColumns());
        System.out.println("getOrderByColumns : " + visitor.getOrderByColumns());
        System.out.println("getVariants : " + visitor.getVariants());
        System.out.println("getParameters : " + visitor.getParameters());

        for (TableStat.Column column : visitor.getColumns()) {
            System.out.println(column.getName());
            column.setName(column.getName() + "_");
            System.out.println(column.getName() + "   a");
        }

        System.out.println("===============================");
        Map<TableStat.Name, TableStat> tables = visitor.getTables();
        for (Map.Entry<TableStat.Name, TableStat> entry : tables.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
            System.out.println("=================================");
        }

        String sql2 = "SELECT UUID();";

        // parser得到AST
        //SQLStatementParser parser2 = new MySqlStatementParser(sql);
        //List<SQLStatement> stmtList = parser.parseStatementList();

        // 将AST通过visitor输出
        StringBuilder out = new StringBuilder();
        MySqlOutputVisitor visitor2 = new MySqlOutputVisitor(out);

        for (SQLStatement stmt : statementList) {
            stmt.accept(visitor2);
            out.append(";");
        }

        //System.out.println(out.toString() + "  aaa");

        String path = "C:\\Users\\Jackie Liu\\Desktop\\安徽省.json";
        String content = FileUtils.readTxt(path);

        List<Area> list = JSONArray.parseArray(content, Area.class);
        StringBuilder stringBuilder = new StringBuilder();

        Area parent = new Area();
        parent.setId(KeyUtils.getDistributedId());
        parent.setParentId(1L);
        parent.setIdPath(parent.getId().toString());
        parent.setName("安徽省");
        parent.setCode("340000000000");
        parent.setType("PROVINCE");

        setIdPath(list, parent);

        list.add(parent);
        sql(list, stringBuilder);
        FileUtils.writeTxt(stringBuilder.toString(), "C:\\Users\\Jackie Liu\\Desktop\\area.sql");
        //System.out.println(list.size());
    }

    public static final String insert = "INSERT INTO TC_AREA (ID_, PARENT_ID_, NAME_, ORDER_BY_, CODE_, CATEGORY_, STATE_, ID_PATH_) VALUES (${ID}, ${PARENT_ID}, '${NAME}', 0, '${destr}', ${tegorystr}, 1, '${ID_PATH}');";

    public static void sql(List<Area> list, StringBuilder stringBuilder) {
        for (Area child : list) {
            //System.out.println(child.getId());
            Map<String, String> map = new HashMap<>();
            map.put("ID", child.getId().toString());
            map.put("PARENT_ID", child.getParentId().toString());
            map.put("NAME", child.getName());
            map.put("destr", child.getCode());
            map.put("tegorystr", String.valueOf(child.getType()));
            map.put("ID_PATH", child.getIdPath());

            String sql = FormatUtils.formatCharSequence(insert, map);
            stringBuilder.append(sql).append("\n");
            System.out.println(sql);
            if (child.getChildren() != null) {
                sql(child.getChildren(), stringBuilder);
            }
        }
    }

    public static void setIdPath(List<Area> list, Area parent) {
        for (Area child : list) {
            child.setId(KeyUtils.getDistributedId());
            if (parent == null) {
                child.setIdPath(child.getId().toString());
                child.setParentId(1L);
            } else {
                child.setParentId(parent.getId());
                child.setIdPath(parent.getIdPath() + "/" + child.getId().toString());
            }
            if (child.getChildren() != null) {
                setIdPath(child.getChildren(), child);
            }
        }
    }

    public static void setIdPath(List<JSONObject> list, List<Area> areaList, Area parent) {
        for (JSONObject json : list) {
            Area child = json.toJavaObject(Area.class);
            child.setChildren(new ArrayList<Area>());
            if (parent == null) {
                areaList.add(child);
            } else {
                parent.getChildren().add(child);
            }
            List<JSONObject> jsonObjectList = json.getJSONArray()
        }
    }
*/

    public static void main(String[] args) {
        System.out.println(new BigDecimal("-100").compareTo(BigDecimal.ZERO));
        Date now = new Date();
        Date date = DateUtils.addMoth(now, 1);
        System.out.println(DateUtils.dateToString(date));
    }
}
